<?php

namespace app\common\model;

use think\Model;


class CycleOrderStat extends Model
{

    

    

    // 表名
    protected $name = 'cycle_order_stat';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'cycle_starttime_text',
        'cycle_endtime_text',
        'calc_type_text'
    ];
    

    
    public function getCalcTypeList()
    {
        return ['1' => __('Calc_type 1'), '2' => __('Calc_type 2')];
    }


    public function getCycleStarttimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['cycle_starttime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCycleEndtimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['cycle_endtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCalcTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['calc_type'] ?? '');
        $list = $this->getCalcTypeList();
        return $list[$value] ?? '';
    }

    protected function setCycleStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCycleEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
